package com.zbf.core.exception;

import com.zbf.common.entity.AuthErrorMessage;
import com.zbf.common.entity.ResponseResult;
import com.zbf.common.entity.ResponseResultEnum;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 作者: LCG
 * 日期: 2020/6/1 09:19
 * 描述: 全局异常处理类
 */

@ControllerAdvice
public class GloableExceptionHandler {

   /**
    * 处理Exception
    * @param exception
    * @return
    */
   @ExceptionHandler(Exception.class)
   @ResponseBody
   public ResponseResult exceptionHandler(Exception exception){
      ResponseResult responseResult=ResponseResult.getResponseResult();
      responseResult.setError(exception.getMessage());
      responseResult.setCode(500);
      return responseResult;
   }


   /**
    * 权限异常处理类
    * @param authException
    * @return
    */
   @ExceptionHandler(AuthException.class)
   @ResponseBody
   public ResponseResult authException(AuthException authException){
      ResponseResult responseResult=ResponseResult.getResponseResult();
      responseResult.setError(authException.getMessage());
      responseResult.setCode(AuthErrorMessage.NO_AUTH.getCode());
      return responseResult;
   }


}
